package org.easy.amapiplocation;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

/**
 * 高德地图
 *
 * @author: Li chaojie
 * @Date: 2022/3/21
 * @Description:
 */
@Configuration
@EnableConfigurationProperties({AmapIpProperties.class})
@ConditionalOnProperty(prefix = AmapIpProperties.PREFIX, name = "enabled", havingValue = "true")
@AutoConfigureAfter
@Slf4j
public class AmapIpAutoConfiguration implements InitializingBean {

    @Resource
    private AmapIpProperties amapIpProperties;


    @Bean
    @ConditionalOnMissingBean
    public AmapIpTemplate ipTemplate() {
        return new AmapIpTemplate();
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        checkParam();
    }

    private void checkParam() {
        if (amapIpProperties.getKey() == null) {
            log.error("未配置" + AmapIpProperties.class.getTypeName());
        }

    }
}
